package com.hs.service.impl;

import com.hs.entity.TbMenu;
import com.hs.mapper.TbMenuMapper;
import com.hs.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {
    /*获取页面菜单*/
    @Autowired
    private TbMenuMapper tbMenuMapper;

    @Override
    public Map<String, Object> getMenus(Integer roleId) {
        if(roleId!=null){
            try{
                //查询当前角色的菜单列表
                List<Map<String,Object>> menus = tbMenuMapper.queryMenuByRoleId(roleId);
                //创建最外层Map
                Map<String,Object> menuMap = new HashMap<>();
                Map<String,Object> homeInfo = new HashMap<>();
                homeInfo.put("title","首页");
                homeInfo.put("href","index/welcome");
                //将HomeInfo放到外层map中
                menuMap.put("homeInfo",homeInfo);
                /*
                LogoInfo信息
                 */
                Map<String,Object> logoInfo = new HashMap<>();
                logoInfo.put("title","教务管理系统");
                logoInfo.put("image","images/logo.png");
                logoInfo.put("href","");
                //将LogoInfo放到外层map中
                menuMap.put("logoInfo",logoInfo);
                /*
                MenuInfo信息
                 */
                Map<String,Object> menuInfo = new HashMap<>();

                //判断角色名称
                String roleName = roleId==1?"管理员":roleId==2?"教师":"学生";
                menuInfo.put("title",roleName);
                menuInfo.put("icon","fa fa-address-book");
                menuInfo.put("href","");
                menuInfo.put("target","_self");
                menuInfo.put("child",menus);
                //将menuInfo放进外层map中
                Object[] objs = new Object[1];
                objs[0] = menuInfo;
                menuMap.put("menuInfo", objs);
                //返回菜单map
                return menuMap;
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }else{
            return null;
        }
    }

    @Override
    public String queryMenuIdsByRoleId(Integer roleId) throws SQLException {
        Map<String,Object> map = tbMenuMapper.queryMenuIdsByRoleId(roleId);
        if(map!=null && map.get("menuIds")!=null){
            return map.get("menuIds").toString();
        }
        return "";
    }

    @Override
    public Integer deleteById(Integer id) throws SQLException {
        return tbMenuMapper.deleteById(id);
    }
    @Override
    public Integer deleteBatch(String ids) throws SQLException {
        return tbMenuMapper.deleteBatch(ids);
    }
    @Override
    public Integer createMenu(String name) throws SQLException {
        return tbMenuMapper.createMenu(name);
    }
    @Override
    public TbMenu queryById(Integer id) throws SQLException {
        return tbMenuMapper.queryById(id);
    }
    @Override
    public Integer updateMenu(Map<String, String[]> map) throws SQLException {
        return tbMenuMapper.updateMenu(map);
    }

    @Override
    public List<Map<String,Object>> queryAll() {
        return tbMenuMapper.queryAll();
    }
}
